package com.zhongketech.sunflower.activity.me;

import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.activity.BaseActivity;
import com.zhongketech.sunflower.transport.Action;
import com.zhongketech.sunflower.transport.model.User;

import android.app.ProgressDialog;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class ModifyGenderActivity extends BaseActivity implements OnClickListener{

	public final static String TAG = "ModifyGenderActivity";
	private ImageView femaleEnable;
	private ImageView maleEnable;
	private Resources rs;
	private String nowState;
	private String maleStr;
	private String femaleStr;
	private Handler handler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_gender);

		handler = new Handler() {
			public void handleMessage(Message msg) {
				finish();
			}
		};
		rs = getResources();
		maleStr = "male";
		femaleStr = "female";

		initHeadTitle();
		initComponent();
	}


	private void initComponent() {
		femaleEnable = (ImageView)findViewById(R.id.enable_female);
		maleEnable = (ImageView)findViewById(R.id.enable_male);

		findViewById(R.id.female).setOnClickListener(this);
		findViewById(R.id.male).setOnClickListener(this);

		String gender = getIntent().getExtras().getString("gender");
		setGenderState(gender);
		nowState = gender;
	}

	private void initHeadTitle() {
		//		TextView back = (TextView) findViewById(R.id.back);
		TextView title = (TextView) findViewById(R.id.title);
		TextView other = (TextView) findViewById(R.id.other);

		title.setText("性别修改");
		//		back.setOnClickListener(this);
		other.setVisibility(View.VISIBLE);
		other.setText(rs.getString(R.string.save));
		other.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.other:
			updateUserInfo();
			break;
		case R.id.male:
			setGenderState(maleStr);
			break;
		case R.id.female:
			setGenderState(femaleStr);
			break;
		default:
			break;
		}
	}


	/**
	 * 失去焦点
	 */
	public void loseFocus(View view) {	
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.requestFocus();
	}

	private void updateUserInfo() {
		final ProgressDialog pd = new ProgressDialog(this);
		pd.setCanceledOnTouchOutside(false);
		pd.setMessage("正在修改...");
		pd.show();

		new Thread(new Runnable() {
			public void run() {
				Looper.prepare();

				User tmpUser = MyApplication.getInstance().getUser();
				tmpUser.setGender(nowState);
				User newUser = new Action().ModifyMe(tmpUser);
				if(null != newUser)
				{
					final Message msg = new Message();
					handler.sendMessage(msg);
					newUser.setPassword(tmpUser.getPassword());
					MyApplication.getInstance().SetUser(newUser);
					MyToast.getInstance().show("更新成功");
				}
			}
		}).start();
	}

	private void setGenderState(String gender) {
		if(gender.equals(femaleStr)){
			femaleEnable.setVisibility(View.VISIBLE);
			maleEnable.setVisibility(View.INVISIBLE);
			nowState = femaleStr;
		}else{
			femaleEnable.setVisibility(View.INVISIBLE);
			maleEnable.setVisibility(View.VISIBLE);
			nowState = maleStr;
		}
	}

}
